import { Pubsub } from '@/plugins/pubsub'
import { DOC_CTRL_S, DOC_CTRL_ALT_N } from '@/types/constant'

function injectKeyboardEvent() {
  document.addEventListener('keydown', handleKeyboardEvent)
}

function handleKeyboardEvent(e: KeyboardEvent) {
  // 保存
  if (e.key === 's' && e.ctrlKey === true) {
    Pubsub.emit(DOC_CTRL_S, e)

    e.preventDefault()
    return false
  }

  // 新建文件
  if (e.key === 'n' && e.ctrlKey === true && e.altKey === true) {
    Pubsub.emit(DOC_CTRL_ALT_N, e)

    e.preventDefault()
    return false
  }
}

export { injectKeyboardEvent }
